﻿using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;

namespace ImmovablesManager
{
    public delegate void RealtorsInfoChanged(Guid currentGuid);
    /// <summary>
    /// Interaction logic for Realtors.xaml
    /// </summary>
    public partial class Realtors : Window
    {
        
        public Realtors()
        {
            InitializeComponent();

            AppController.realtorsDataGridDataViews = new ObservableCollection<RealtorsDataGridDataView>();

            foreach (var realtor in AppController.ImmoRepository.GetAllUsers())
            {
                AppController.realtorsDataGridDataViews.Add(new RealtorsDataGridDataView()
                                                  {
                                                      RealtorId = realtor.UserId,
                                                      RealtorAdditionalPhone = realtor.UserPhoneNumber,
                                                      RealtorEmail = realtor.UserEmail,
                                                      RealtorFirstName = realtor.UserFirstName,
                                                      RealtorOrganization = realtor.UserOrganization,
                                                      RealtorPhone = realtor.UserPhoneNumber,
                                                      RealtorSecondName = realtor.UserLastName,
                                                      RealtorThirdName = realtor.UserThirdName,
                                                      RealtorUserName = realtor.UserLogin
                                                  });
            }

            RealtorsMainGrid.ItemsSource = AppController.realtorsDataGridDataViews;
        }

        private void AddRealtorBtn_Click(object sender, RoutedEventArgs e)
        {
            var realtorForm = new RealtorsFormEdit(true, new Guid());
            realtorForm.ShowDialog();
        }

        private void DeleteRealtorBtn_Click(object sender, RoutedEventArgs e)
        {
            if (RealtorsMainGrid.SelectedCells.Count > 0)
            {
                var currentItem = RealtorsMainGrid.SelectedCells[0].Item as RealtorsDataGridDataView;
                var guid = new Guid();
                if (currentItem != null)
                {
                    guid = currentItem.RealtorId;
                }

                //if(AppController._immoRepository.HasRealtorFinancialInfo(guid))
                //{
                //    MessageBox.Show("", "", MessageBoxButton.OK, MessageBoxImage.Exclamation);
                //}


                AppController.realtorsDataGridDataViews.Remove(currentItem);
                AppController.ImmoRepository.RemoveRealtor(guid);
                return;
            }
            MessageBox.Show("Ошибка!","Выберите риэлтора для совершения операции!");
        }

        private void EditRealtorBtn_Click(object sender, RoutedEventArgs e)
        {
            if (RealtorsMainGrid.SelectedCells.Count > 0)
            {
                var currentItem = RealtorsMainGrid.SelectedCells[0].Item as RealtorsDataGridDataView;
                var guid = new Guid();
                if (currentItem != null)
                {
                    guid = currentItem.RealtorId;
                }

                var realtorForm = new RealtorsFormEdit(false, guid);
                realtorForm.ShowDialog();
            }
            else MessageBox.Show("Ошибка!", "Выберите риэлтора для совершения операции!");
        }

    }
}
